package com.dingpeikeji.quickdeer.modules.rentproduct.web;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotypeForsale;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeForsaleService;

/**
 * 可售数量维护Controller
 * @author mash
 * @version 2017-01-23
 */
@Controller
@RequestMapping(value = "${adminPath}/rentproduct/tCargotypeForsale")
public class TCargotypeForsaleController extends BaseController {

	@Autowired
	private TCargotypeForsaleService tCargotypeForsaleService;
	
	@ModelAttribute
	public TCargotypeForsale get(@RequestParam(required=false) String id) {
		TCargotypeForsale entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tCargotypeForsaleService.get(id);
		}
		if (entity == null){
			entity = new TCargotypeForsale();
		}
		return entity;
	}
	
	@RequiresPermissions("rentproduct:tCargotypeForsale:view")
	@RequestMapping(value = {"list", ""})
	public String list(TCargotypeForsale tCargotypeForsale, HttpServletRequest request, HttpServletResponse response, Model model) {
		if (tCargotypeForsale.getOffice() == null)
			tCargotypeForsale.setOffice(UserUtils.getUser().getOffice());
		
		Page<TCargotypeForsale> page = tCargotypeForsaleService.findPage(new Page<TCargotypeForsale>(request, response), tCargotypeForsale); 
		model.addAttribute("page", page);
		return "modules/rentproduct/tCargotypeForsaleList";
	}
	
	@RequiresPermissions("plat:tCargotypeForsale:view")
	@RequestMapping(value = {"alllist"})
	public String alllist(TCargotypeForsale tCargotypeForsale, HttpServletRequest request, HttpServletResponse response, Model model) {	
		Page<TCargotypeForsale> page = tCargotypeForsaleService.findPage(new Page<TCargotypeForsale>(request, response), tCargotypeForsale); 
		model.addAttribute("page", page);
		return "modules/rentproduct/tForsaleAllList";
	}

	@RequiresPermissions("rentproduct:tCargotypeForsale:view")
	@RequestMapping(value = "form")
	public String form(TCargotypeForsale tCargotypeForsale, Model model) {
		model.addAttribute("tCargotypeForsale", tCargotypeForsale);
		return "modules/rentproduct/tCargotypeForsaleForm";
	}

	@RequiresPermissions("rentproduct:tCargotypeForsale:edit")
	@RequestMapping(value = "save")
	public String save(TCargotypeForsale tCargotypeForsale, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tCargotypeForsale)){
			return form(tCargotypeForsale, model);
		}
		tCargotypeForsaleService.save(tCargotypeForsale);
		addMessage(redirectAttributes, "保存可销售数量成功");
		return "redirect:"+Global.getAdminPath()+"/rentproduct/tCargotypeForsale/?repage";
	}
	
	@RequiresPermissions("rentproduct:tCargotypeForsale:edit")
	@RequestMapping(value = "delete")
	public String delete(TCargotypeForsale tCargotypeForsale, RedirectAttributes redirectAttributes) {
		tCargotypeForsaleService.delete(tCargotypeForsale);
		addMessage(redirectAttributes, "删除可销售数量成功");
		return "redirect:"+Global.getAdminPath()+"/rentproduct/tCargotypeForsale/?repage";
	}

}